package com.yxuan.springboot.service.impl;

import com.yxuan.springboot.mapper.*;
import com.yxuan.springboot.model.*;
import com.yxuan.springboot.mapper.*;
import com.yxuan.springboot.model.*;
import com.yxuan.springboot.service.TestRecordsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.*;

/**
 * 
 * @author zchao
 *    TestRecords业务层实现类
 * @date 2023-05-16 14:22:56
 */
@Service
@Transactional
public class TestRecordsServiceImpl extends BaseServiceImpl<TestRecords> implements TestRecordsService {


    @Autowired
    private TestRecordsMapper testRecordsMapper;

    @Autowired
    private TestAnswerMapper testAnswerMapper;

    @Autowired
    private TestOptionsMapper testOptionsMapper;

    @Autowired
    private TestTypeDetailsMapper testTypeDetailsMapper;


    @Autowired
    private TestVersionTopicMapper testVersionTopicMapper;

    @Autowired
    private TestQuestionsMapper testQuestionsMapper;

    @Autowired
    private TestScorneirongMapper testScorneirongMapper;
//   TODO 创建测试记录
    @Override
    public HashMap<String, Object> createRecords(TestRecords entity, List<Map> list) throws Exception {
        HashMap<String, Object> result = new HashMap<>();
        Date date = new Date();
        Timestamp timestamp = new Timestamp(date.getTime());
        entity.setSubmitTime(timestamp);
        testRecordsMapper.insert(entity);
        TestRecords testRecords = testRecordsMapper.queryObjectByPramas(entity);

//        计算成绩
//        未完成但保存
        if (entity.getStatus().equals("0")){
            for (Map map : list){
                TestAnswer testAnswer = new TestAnswer();
                testAnswer.setUserId(testRecords.getUserId());
                testAnswer.setTestRecordsId(testRecords.getId());
                testAnswer.setTopicId((Integer) map.get("id"));
                testAnswer.setAnswer((String) map.get("respondence"));
                testAnswerMapper.insert(testAnswer);
            }
            entity.setGrade(0f);
            testRecordsMapper.update(entity);
            result.put("result","success");
            TestRecords testRecords1 = testRecordsMapper.queryObjectByPramas(entity);
            result.put("recordid",testRecords.getId());
            return result;
        }

//        已完成,计算成绩
        float totalScore=0;
        for (Map map : list) {
            TestAnswer testAnswer = new TestAnswer();
            testAnswer.setUserId(testRecords.getUserId());
            testAnswer.setTestRecordsId(testRecords.getId());
            testAnswer.setTopicId((Integer) map.get("id"));
            testAnswer.setAnswer((String) map.get("respondence"));
            testAnswerMapper.insert(testAnswer);
//            计算成绩
            TestOptions options = new TestOptions();
            options.setQuestionId(testAnswer.getTopicId());
            options.setOptionName(testAnswer.getAnswer());
            TestOptions objectByPramas = testOptionsMapper.queryObjectByPramas(options);
            totalScore += objectByPramas.getOptionScore();
        }
        entity.setGrade(totalScore);
        testRecordsMapper.update(entity);
//        TestScorneirong testScorneirong = new TestScorneirong();
//        testScorneirong.setVersion(entity.getVersion());
//        testScorneirong.setTypeId(entity.getTypeId());
//        List<TestScorneirong> testScorneirongs = testScorneirongMapper.queryListByPramas(testScorneirong);
//        for (TestScorneirong scorneirong : testScorneirongs){
//            if (entity.getGrade()>scorneirong.getScoreHead() && entity.getGrade()<scorneirong.getScoreEnd()){
//                testScorneirong.setScoreHead(scorneirong.getScoreHead());
//                break;
//            }
//        }
//        TestScorneirong scorneirong = testScorneirongMapper.queryObjectByPramas(testScorneirong);
        result.put("record_id",testRecords.getId());
        result.put("result","success");
        return result;
    }

//    查看测试记录详情
    @Override
    public HashMap<String, Object> recordDetail(TestRecords entity) throws Exception {
        HashMap<String,Object> result = new HashMap<>();
        TestRecords testRecords = testRecordsMapper.queryObjectByPramas(entity);
        TestTypeDetails testTypeDetails = new TestTypeDetails();
        testTypeDetails.setId(testRecords.getTypeId());
        System.out.println(testTypeDetails);
        TestTypeDetails testTypeDetail = testTypeDetailsMapper.queryObjectByPramas(testTypeDetails);
        result.put("id",testRecords.getTypeId());
        result.put("testname",testTypeDetail.getTypeName());
        result.put("introduce", testTypeDetail.getIntroduce());
        result.put("pic",testTypeDetail.getPic());
//        test_version_type:根据typeid查找version
        TestTypeDetails testTypeDetails1 = testTypeDetailsMapper.queryObjectById(String.valueOf(testRecords.getTypeId()));

        result.put("topic_number",testTypeDetails1.getTopicNum());
        result.put("test_time",testTypeDetails1.getSuggestTime());
        result.put("illustrate",testTypeDetails1.getTypeExplain());
        result.put("version",testTypeDetails1.getVersion());
//        TODO 使用treemap有序存储题目以及答案
//        通过 version 和 typeid 找到topic_id
        TestVersionTopic testVersionTopic = new TestVersionTopic();
        testVersionTopic.setVersion(testTypeDetails1.getVersion());
        testVersionTopic.setTypeId(testRecords.getTypeId());
        List<TestVersionTopic> testVersionTopicslist = testVersionTopicMapper.queryListByPramas(testVersionTopic);
//        遍历列表查找题目,
        List<Object> list = new ArrayList<>();
        for (int i = 0; i < testVersionTopicslist.size();i++){
            HashMap<Object, Object> result1 = new HashMap<>();
            TestVersionTopic testVersionTopic1 = testVersionTopicslist.get(i);

            TestQuestions testQuestion = new TestQuestions();
            testQuestion.setId(testVersionTopic1.getTopicId());
            TestQuestions testQuestion1 = testQuestionsMapper.queryObjectByPramas(testQuestion);
            result1.put("id",testQuestion1.getId());
            result1.put("content",testQuestion1.getContent());
            result1.put("question_pic",testQuestion1.getQuestionPic());
//            根据题目id、userid、记录id查找题目对应的选项
            TestAnswer testAnswer = new TestAnswer();
            testAnswer.setUserId(entity.getUserId());
            testAnswer.setTopicId(testQuestion1.getId());
            testAnswer.setTestRecordsId(testRecords.getId());
            TestAnswer testAnswer1 = testAnswerMapper.queryObjectByPramas(testAnswer);
            result1.put("respondence",testAnswer1.getAnswer());
//            通过题目id来查找答案
            TestOptions option = new TestOptions();
            option.setQuestionId(testQuestion1.getId());
            List<TestOptions> testOptionsList = testOptionsMapper.queryListByPramas(option);
//            排序
            for(int m = 0;m < testOptionsList.size()-1;m++){
                for (int n = m+1;n < testOptionsList.size();n++){
                    int i1 = (testOptionsList.get(m).getOptionName().charAt(0) - '0');
                    int i2 = (testOptionsList.get(n).getOptionName().charAt(0) - '0');
                    if (i1 > i2){
                        TestOptions options = testOptionsList.get(m);
                        testOptionsList.set(m,testOptionsList.get(n));
                        testOptionsList.set(n,options);
                    }
                }
            }


            List<Object> list1 = new ArrayList<>();
            for(TestOptions j : testOptionsList){
                TreeMap<String, Object> result2 = new TreeMap<>();
                result2.put("id",j.getId());
                result2.put("optionName",j.getOptionName());
                result2.put("optionScore",j.getOptionScore());
                result2.put("optionContent",j.getOptionContent());
                list1.add(result2);
            }
            result1.put("options",list1);
            list.add(result1);
        }
        result.put("title",list);
        return result;
    }



}
